package mysql

import "sync"

// DoBatch 最多有goroutineNum个协程批量执行
func DoBatch(max int, goroutineNum int, execFunc func(index int)) {
	var (
		wg          sync.WaitGroup
		workLimiter = make(chan struct{}, goroutineNum)
	)

	for i := 0; i < max; i++ {
		wg.Add(1)
		select {
		case workLimiter <- struct{}{}:
			go func(i int) {
				defer func() {
					<-workLimiter //同时最多有goroutineNum个协程在跑
					wg.Done()
				}()
				execFunc(i)
			}(i)
		}
	}
	wg.Wait()
}
